package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.OrderBean;
import com.wuliupai.delivery.wuliupaidelivery.utils.CityUtil;

import java.util.List;

/**
 * Descriptions：快速下单列表的Adapter
 * <p>
 * Author：ChenME
 * Date：2017/1/7
 * Email：ibelieve1210@163.com
 */
public class FastOrderListAdapter extends BaseAdapter {
    private List<OrderBean> dataList;
    private Context context;
    private int whichPage;
    private CityUtil cityUtil;

    public FastOrderListAdapter(Context context, List<OrderBean> dataList, int whichPage) {
        this.context = context;
        this.dataList = dataList;
        this.whichPage = whichPage;
        cityUtil = new CityUtil(context);
    }

    @Override
    public int getCount() {
        return null != dataList ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_fast_order_lv, null);
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.tv_fromInfo = (TextView) convertView.findViewById(R.id.tv_fromInfo);
            holder.tv_fromPersonInfo = (TextView) convertView.findViewById(R.id.tv_fromPersonInfo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        try {
            String address = "";
            String personInfo = "";
            OrderBean orderBean = dataList.get(position);
            if (3 == whichPage) {
                //快速下单
                address = cityUtil.findProvinceName(orderBean.getStartProvince()) +
                        cityUtil.findCityName(orderBean.getStartCity()) +
                        cityUtil.findAreaName(orderBean.getStartArea()) +
                        orderBean.getStartAddress();
                personInfo = orderBean.getConsignorName() + " " + orderBean.getConsignorMobile();
            } else {
                if (1 == whichPage) {
                    //常用收货信息
                    holder.iv_icon.setImageResource(R.mipmap.com_closed);
                } else if (2 == whichPage) {
                    //常用发货信息
                    holder.iv_icon.setImageResource(R.mipmap.com_hair);
                }
                address = cityUtil.findProvinceName(orderBean.getProvinceCode()) +
                        cityUtil.findCityName(orderBean.getCityCode()) +
                        cityUtil.findAreaName(orderBean.getAreaCode()) +
                        orderBean.getAddress();
                personInfo = orderBean.getName() + " " + orderBean.getMobile();
            }
            holder.tv_fromInfo.setText(address);

            holder.tv_fromPersonInfo.setText(personInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        ImageView iv_icon;
        TextView tv_fromInfo;
        TextView tv_fromPersonInfo;
    }
}